﻿using Amazon.Runtime.Internal.Util;
using MediatR;
using PublicCommon;
using System.Diagnostics.Metrics;
using System.Linq.Expressions;
using T_Material.Domain;
using T_Material.Infrastructure.ImpI;
using T_Material.Infrastructure.Interface;
using T_Material.Read.API.Application.Commands.RequisitionCommand;
using T_Material.Read.API.Application.Commands.StatisticsCommand;

namespace T_Material.Read.API.Application.CommandHandlers.StatisticsHandler
{
    public class StatisticsListCommandHandler : IRequestHandler<StatisticsListCommand, PageResult<Statistics>>
    {
        private readonly IStatisticsRepository statisticsRepository;
        private readonly IRequisitionRepository requisitionRepository;
        private readonly IPurchaseRepository purchaseRepository;

        public StatisticsListCommandHandler(IStatisticsRepository statisticsRepository, IRequisitionRepository requisitionRepository, IPurchaseRepository purchaseRepository)
        {
            this.statisticsRepository = statisticsRepository;
            this.requisitionRepository = requisitionRepository;
            this.purchaseRepository = purchaseRepository;
        }
        public async Task<PageResult<Statistics>> Handle(StatisticsListCommand request, CancellationToken cancellationToken)
        {
            var ww = requisitionRepository.GetList().Result.Where(x => !x.IsDel).AsQueryable();

            var ss = purchaseRepository.GetList().Result.Where(x => !x.IsDel).AsQueryable();

            var list = statisticsRepository.GetList().Result.Where(x => !x.IsDel).AsQueryable();
            var qwery = from bb in ss
                        join aa in list on bb.GoodsName equals aa.GoodsName
                        join cc in ww on aa.GoodsName equals cc.GoodsName
                        select new StaisticsDto
                        {
                            Id = aa.Id,
                            //detailsId= aa.detailsId,
                            GoodsName = aa.GoodsName,
                            TypeName = aa.TypeName,
                            Specifications = aa.Specifications,
                            Country = aa.Country,
                            Quantity = bb.Quantity,
                            ReceivedCount = cc.ReceivedCount,
                            SurplusCount = aa.SurplusCount,
                            MaterialQuality = aa.MaterialQuality,
                            StartTime = aa.StartTime,
                            EndTime = aa.EndTime,
                        };

            if (!string.IsNullOrEmpty(request.GoodsName))
            {
                list = list.Where(x => x.GoodsName.Contains(request.GoodsName));
            }
            //if (!string.IsNullOrEmpty(request.MaterialQuality))
            //{
            //    list = list.Where(x => x.MaterialQuality.Contains(request.MaterialQuality));
            //}
            if (!string.IsNullOrEmpty(request.Country))
            {
                list = list.Where(x => x.Country.Contains(request.Country));
            }
            //if (request.StartTime >= request.StartTime && request.EndTime <= request.EndTime)
            //{

            //}
            if (!string.IsNullOrEmpty(Convert.ToString(request.StartTime)))
            {
                list = list.Where(x => x.StartTime >= request.StartTime);
            }
            if (!string.IsNullOrEmpty(Convert.ToString(request.EndTime)))
            {
                list = list.Where(x => x.EndTime <= request.EndTime);
            }

            int totalCount = list.Count();
            int pageCount = (int)Math.Ceiling(totalCount * 1.0 / request.pageSize);
            var pagelist = list.OrderByDescending(x => x.Id).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
            return new PageResult<Statistics>
            {
                List = pagelist,
                PageCount = pageCount,
                TotalCount = totalCount
            };
        }
    }
}
